package JavaSEAdvance.day07_Exception;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author Xiao
 * @date 2021/7/24
 */
public class D704_IteratorDemo03 {
    /**
     * 思考题：迭代器中加if判断后使用集合对象调用remove方法为何没报并发修改异常？
     */
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>();
//        method1(c);
        c.add("a");
        c.add("b");
        c.add("c");
        c.add("d");
        Iterator<String> i = c.iterator();
        while (i.hasNext()) {
            if (i.next().equals("c")) {
//                c.remove(i.next());     //ConcurrentModificationException
                c.remove("c");
            }
        }
        System.out.println(c);
    }

    private static void method1(Collection<String> c) {
        c.add("张三丰");
        c.add("张无忌");
        c.add("李世民");
        c.add("阳顶天");
        Iterator<String> i = c.iterator();
        while (i.hasNext()) {
            if (i.next().equals("李世民")) {
                c.remove("李世民");
            }
        }
        System.out.println(c);
    }
}
